package com.ln.lnzw.net;


import com.ln.lnzw.app.AppConstant;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;

/**
 * Created by wuwei on 2017/8/25.
 * //接口返回字符串
 */
public class HttpMethodString {

    public static final String BASE_URL = AppConstant.WCM_NEWS_BASE_URL;
    public static final String BASE_URL_PERSON = AppConstant.BASE_URL;

    private static final int DEFAULT_TIMEOUT = 60;
    private Retrofit retrofit;
    public RequestInfonApi info;

    /**
     * 构造方法私有
     */
    private HttpMethodString() {
        //手动创建一个OkHttpClient并设置超时时间
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        builder.connectTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
                .readTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
                .writeTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS);
        builder.retryOnConnectionFailure(true);
        retrofit = new Retrofit.Builder()
                .client(builder.build())
                .addConverterFactory(ScalarsConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .baseUrl(BASE_URL)
                .build();
        info = retrofit.create(RequestInfonApi.class);

    }
    //构造方法私有 个人中心   s：传个人中心的baseurl
    private HttpMethodString(String url) {
        //手动创建一个OkHttpClient并设置超时时间
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        builder.connectTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
                .readTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
                .writeTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS);
        builder.retryOnConnectionFailure(true);
        retrofit = new Retrofit.Builder()
                .client(builder.build())
                .addConverterFactory(ScalarsConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .baseUrl(url)
                .build();
        info = retrofit.create(RequestInfonApi.class);

    }

    /**
     * 在访问HttpMethods时创建单例
     */
    private static class SingletonHolder {
        private static final HttpMethodString INSTANCE = new HttpMethodString();
    }
    //在访问HttpMethods时创建单例
    private static class SingletonHolder1 {
        private static final HttpMethodString INSTANCE_CENTER = new HttpMethodString(BASE_URL_PERSON);
    }
    /**
     * 获取单例
     *
     * @return
     */
    public static HttpMethodString getInstance() {
        return SingletonHolder.INSTANCE;
    }

    public static HttpMethodString getInstanceCenter() {
        return SingletonHolder1.INSTANCE_CENTER;
    }
}
